{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "e53098fc-9b14-4d2d-a318-211e685ee2c5",
   "metadata": {},
   "source": [
    "# Pandas加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "1f5a1373-2483-424e-9265-6752443c6c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eec9d8db-0248-4847-9456-7fb6bd2b9480",
   "metadata": {},
   "source": [
    "**csv数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "1ed727fe-448f-4819-bc20-f21456d4452b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Scala</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>15</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "      <td>46</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>47</td>\n",
       "      <td>32</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>36</td>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>14</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>49</td>\n",
       "      <td>13</td>\n",
       "      <td>4</td>\n",
       "      <td>40</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>42</td>\n",
       "      <td>26</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>31</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>45</td>\n",
       "      <td>23</td>\n",
       "      <td>5</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Java  NumPy  Pandas  Scala\n",
       "0      15    36      3      43     39\n",
       "1       7     9     24      15     12\n",
       "2      37    40     14      46     17\n",
       "3      10    29     47      32      5\n",
       "4      23     3     22      44     40\n",
       "5      36    33     23      14     31\n",
       "6      49    13      4      40     47\n",
       "7      42    26     30      36     25\n",
       "8      31    40      1      20     34\n",
       "9      45    23      5      40     41"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,50,size=(10,5))\n",
    "df = pd.DataFrame(data=data,columns=['Python','Java','NumPy','Pandas','Scala'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8040c7cb-df3b-4342-80b6-fc6084aa57ff",
   "metadata": {},
   "source": [
    "- df.to_csv:保存到csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "60a5f561-2a4d-418e-923f-8362860509fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sep:分隔符默认','\n",
    "# header:是否保存列索引\n",
    "# index:是否保留行索引\n",
    "df.to_csv('data_csv.csv',sep=',',header=True,index=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a4c96a6-53e5-46b8-9283-d2936f2c7a43",
   "metadata": {},
   "source": [
    "- df.read_csv:加载csv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "f61e8390-ae54-4936-9f03-f6592aa2f1e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Scala</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>15</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "      <td>46</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>47</td>\n",
       "      <td>32</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>36</td>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>14</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>49</td>\n",
       "      <td>13</td>\n",
       "      <td>4</td>\n",
       "      <td>40</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>42</td>\n",
       "      <td>26</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>31</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>45</td>\n",
       "      <td>23</td>\n",
       "      <td>5</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Java  NumPy  Pandas  Scala\n",
       "0      15    36      3      43     39\n",
       "1       7     9     24      15     12\n",
       "2      37    40     14      46     17\n",
       "3      10    29     47      32      5\n",
       "4      23     3     22      44     40\n",
       "5      36    33     23      14     31\n",
       "6      49    13      4      40     47\n",
       "7      42    26     30      36     25\n",
       "8      31    40      1      20     34\n",
       "9      45    23      5      40     41"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sep默认','\n",
    "# header 指定列索引\n",
    "# index 指定行索引\n",
    "pd.read_csv('data_csv.csv',sep=',',header=[0],index_col=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84fd02a9-8433-4114-9fbc-7e9761719474",
   "metadata": {},
   "source": [
    "不获取列：header=None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "598ebd51-0078-424d-ba23-50a8fc1e3c92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>Python</td>\n",
       "      <td>Java</td>\n",
       "      <td>NumPy</td>\n",
       "      <td>Pandas</td>\n",
       "      <td>Scala</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0.0</th>\n",
       "      <td>15</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1.0</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2.0</th>\n",
       "      <td>37</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "      <td>46</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3.0</th>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>47</td>\n",
       "      <td>32</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4.0</th>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5.0</th>\n",
       "      <td>36</td>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>14</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6.0</th>\n",
       "      <td>49</td>\n",
       "      <td>13</td>\n",
       "      <td>4</td>\n",
       "      <td>40</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7.0</th>\n",
       "      <td>42</td>\n",
       "      <td>26</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8.0</th>\n",
       "      <td>31</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9.0</th>\n",
       "      <td>45</td>\n",
       "      <td>23</td>\n",
       "      <td>5</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          1     2      3       4      5\n",
       "0                                      \n",
       "NaN  Python  Java  NumPy  Pandas  Scala\n",
       "0.0      15    36      3      43     39\n",
       "1.0       7     9     24      15     12\n",
       "2.0      37    40     14      46     17\n",
       "3.0      10    29     47      32      5\n",
       "4.0      23     3     22      44     40\n",
       "5.0      36    33     23      14     31\n",
       "6.0      49    13      4      40     47\n",
       "7.0      42    26     30      36     25\n",
       "8.0      31    40      1      20     34\n",
       "9.0      45    23      5      40     41"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_csv('data_csv.csv',sep=',',header=None,index_col=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee5bd903-0a5c-4253-b493-c8a205fcff19",
   "metadata": {},
   "source": [
    "pd.read_table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3582dc0c-621e-423b-a40d-aba99eb5506b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Scala</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>15</td>\n",
       "      <td>36</td>\n",
       "      <td>3</td>\n",
       "      <td>43</td>\n",
       "      <td>39</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>7</td>\n",
       "      <td>9</td>\n",
       "      <td>24</td>\n",
       "      <td>15</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>37</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "      <td>46</td>\n",
       "      <td>17</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10</td>\n",
       "      <td>29</td>\n",
       "      <td>47</td>\n",
       "      <td>32</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23</td>\n",
       "      <td>3</td>\n",
       "      <td>22</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>36</td>\n",
       "      <td>33</td>\n",
       "      <td>23</td>\n",
       "      <td>14</td>\n",
       "      <td>31</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>49</td>\n",
       "      <td>13</td>\n",
       "      <td>4</td>\n",
       "      <td>40</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>42</td>\n",
       "      <td>26</td>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>31</td>\n",
       "      <td>40</td>\n",
       "      <td>1</td>\n",
       "      <td>20</td>\n",
       "      <td>34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>45</td>\n",
       "      <td>23</td>\n",
       "      <td>5</td>\n",
       "      <td>40</td>\n",
       "      <td>41</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Java  NumPy  Pandas  Scala\n",
       "0      15    36      3      43     39\n",
       "1       7     9     24      15     12\n",
       "2      37    40     14      46     17\n",
       "3      10    29     47      32      5\n",
       "4      23     3     22      44     40\n",
       "5      36    33     23      14     31\n",
       "6      49    13      4      40     47\n",
       "7      42    26     30      36     25\n",
       "8      31    40      1      20     34\n",
       "9      45    23      5      40     41"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用read_table默认分隔符不是','而是'\\t'\n",
    "pd.read_table('data_csv.csv',sep=',',index_col=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59c71cdb-b089-495a-ad20-568d27e2a4e6",
   "metadata": {},
   "source": [
    "**excel数据**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "288e3bca-4c11-4f50-854b-c53fe30239e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Python</th>\n",
       "      <th>Java</th>\n",
       "      <th>NumPy</th>\n",
       "      <th>Pandas</th>\n",
       "      <th>Scala</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>46</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>15</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32</td>\n",
       "      <td>48</td>\n",
       "      <td>29</td>\n",
       "      <td>14</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>27</td>\n",
       "      <td>44</td>\n",
       "      <td>33</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>43</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>37</td>\n",
       "      <td>42</td>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5</td>\n",
       "      <td>22</td>\n",
       "      <td>49</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>26</td>\n",
       "      <td>9</td>\n",
       "      <td>37</td>\n",
       "      <td>49</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>47</td>\n",
       "      <td>47</td>\n",
       "      <td>43</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Python  Java  NumPy  Pandas  Scala\n",
       "0      46    24     30      15     43\n",
       "1      32    48     29      14     27\n",
       "2       7    27     44      33      9\n",
       "3      30    36     17       1     25\n",
       "4       7    43     44      40     29\n",
       "5      37    42      9      30     25\n",
       "6       6     2     23       2      5\n",
       "7       5    22     49      40     14\n",
       "8      26     9     37      49     37\n",
       "9       0    47     47      43     37"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = np.random.randint(0,50,size=(10,5))\n",
    "df = pd.DataFrame(data=data,columns=['Python','Java','NumPy','Pandas','Scala'])\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "091a3416-85b1-4303-b178-b10ec13ae765",
   "metadata": {},
   "source": [
    "- pd.to_excel:保存excel文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "d23907ed-f94d-46f1-a461-e6de40605532",
   "metadata": {},
   "outputs": [],
   "source": [
    "# sheet_name 工作表名称\n",
    "# header 是否保存列索引\n",
    "# index 是否保存行索引\n",
    "df.to_excel('data_excle.xlsx',sheet_name='Sheet1',header=True,index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8f9d77b7-b60a-45a4-9b12-4a044c33acf6",
   "metadata": {},
   "source": [
    "- df.read_excel:读取excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "9825040b-44dc-467e-843d-27446792c722",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "      <th>C</th>\n",
       "      <th>D</th>\n",
       "      <th>E</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>46</td>\n",
       "      <td>24</td>\n",
       "      <td>30</td>\n",
       "      <td>15</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>32</td>\n",
       "      <td>48</td>\n",
       "      <td>29</td>\n",
       "      <td>14</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>7</td>\n",
       "      <td>27</td>\n",
       "      <td>44</td>\n",
       "      <td>33</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>30</td>\n",
       "      <td>36</td>\n",
       "      <td>17</td>\n",
       "      <td>1</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>43</td>\n",
       "      <td>44</td>\n",
       "      <td>40</td>\n",
       "      <td>29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>37</td>\n",
       "      <td>42</td>\n",
       "      <td>9</td>\n",
       "      <td>30</td>\n",
       "      <td>25</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>5</td>\n",
       "      <td>22</td>\n",
       "      <td>49</td>\n",
       "      <td>40</td>\n",
       "      <td>14</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>26</td>\n",
       "      <td>9</td>\n",
       "      <td>37</td>\n",
       "      <td>49</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>47</td>\n",
       "      <td>47</td>\n",
       "      <td>43</td>\n",
       "      <td>37</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    A   B   C   D   E\n",
       "0  46  24  30  15  43\n",
       "1  32  48  29  14  27\n",
       "2   7  27  44  33   9\n",
       "3  30  36  17   1  25\n",
       "4   7  43  44  40  29\n",
       "5  37  42   9  30  25\n",
       "6   6   2  23   2   5\n",
       "7   5  22  49  40  14\n",
       "8  26   9  37  49  37\n",
       "9   0  47  47  43  37"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sheet_name 工作表名称,0表示读取第0个工作表\n",
    "# names 替代列名\n",
    "pd.read_excel('data_excle.xlsx',sheet_name='Sheet1',header=[0])\n",
    "pd.read_excel('data_excle.xlsx',sheet_name=0,header=0,names=list('ABCDE'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
